<?php
include 'api_db.inc';
function doConditionalGet($etag, $lastModified, $expires)
{                                             
	header("Last-Modified: $lastModified");
	header("ETag: \"{$etag}\"");
	header("Expires: $expires");
	$if_none_match = isset($_SERVER['HTTP_IF_NONE_MATCH']) ?
		stripslashes($_SERVER['HTTP_IF_NONE_MATCH']) : 
		false;
	
	$if_modified_since = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ?
		stripslashes($_SERVER['HTTP_IF_MODIFIED_SINCE']) :
		false;
	
	if (!$if_modified_since && !$if_none_match)
		return;
	
	if ($if_none_match && $if_none_match != $etag && $if_none_match != '"' . $etag . '"')
		return; // etag is there but doesn't match
	
	if ($if_modified_since && $if_modified_since != $lastModified)
		return; // if-modified-since is there but doesn't match
	
	// Nothing has changed since their last request - serve a 304 and exit
	header('HTTP/1.1 304 Not Modified');
	exit();
} // doConditionalGet()

function outputBanner($a,$b,$t=0) {
  $fil='./images/bannercache/banner'.md5($a.'/'.$b.'/'.$t);

  if (file_exists($fil)) {
    $thumbModified = filemtime($fil);
    $data  = file_get_contents($fil);
	
    $lastModifiedString	= gmdate('D, d M Y H:i:s', $thumbModified) . ' GMT';
    $expires		= gmdate('D, d M Y H:i:s', $thumbModified+900) . ' GMT';
    $etag				= md5($data);
		
    doConditionalGet($etag, $lastModifiedString,$expires);

    header("Content-type: $mime");
    header('Content-Length: ' . strlen($data));
    echo $data;
    exit();
  }
  if (($_GET['del']==1)&&((rand()%11)==0)) {
  $tt=glob('./images/bannercache/banner*');
  $tt=$tt?$tt:array();
  foreach($tt as $v){
    @unlink($v);
  }
  }

  header("Content-type: image/png");
  if ($t==1) {
    $im     = imagecreatefrompng('./images/banner2.png');
    $black = imagecolorallocate($im, 0, 0, 0);
  } else
  if ($t==2) {
    $black = imagecolorallocate($im, 0, 0, 0);
    $im     = imagecreatefrompng('./images/banner3.png');
  } else
  if ($t==3) {
    $black = imagecolorallocate($im, 1, 5, 86);
    $im     = imagecreatefrompng('./images/banner4.png');
  } else 
  {
    $black = imagecolorallocate($im, 0, 0, 0);
    $im     = imagecreatefrompng('./images/banner.png');
  }


  if ($t!=3) {
    imagettftext($im, 9, 0, 76, 12, $black, './images/banner.ttf',$a);
    imagettftext($im, 9, 0, 124, 12, $black, './images/banner.ttf',$b);
  } else {
    $black2 = imagecolorallocate($im, 1, 5, 86);
    imagettftext($im, 9, 0, 68, 27, $black2, './images/banner2.ttf',$a. '/'. $b);
  }
  imagepng($im);
  imagepng($im,$fil);


  imagedestroy($im);
  
}
if ($r=vk_db_usersCount())  {
if (rand()%10==0) {
  @$f=fopen('./banner_stat.inc','a');
  if ($f) {
    @fputs($f,1);
    @fclose($f);
  }
}
outputBanner($r[0],$r[1],intval($_GET['t']));
} else {
  header('HTTP/1.1 404 Not Found');
}
?>